package com.jq.jqaicode.common.langgraphDemo;

import org.bsc.langgraph4j.StateGraph;
import org.bsc.langgraph4j.GraphStateException;

import static org.bsc.langgraph4j.action.AsyncNodeAction.node_async;
import static org.bsc.langgraph4j.StateGraph.START;
import static org.bsc.langgraph4j.StateGraph.END;

import java.util.Map;

public class SimpleGraphAppDemo {

    public static void main(String[] args) throws GraphStateException {
        // 初始化节点
        GreeterNode greeterNode = new GreeterNode();
        ResponderNode responderNode = new ResponderNode();

        // 定义图形结构
       var stateGraph = new StateGraph<>(SimpleState.SCHEMA, initData -> new SimpleState(initData))
            .addNode("greeter", node_async(greeterNode))
            .addNode("responder", node_async(responderNode))
            // Define edges
            .addEdge(START, "greeter") // Start with the greeter node
            .addEdge("greeter", "responder")
            .addEdge("responder", END)   // End after the responder node
             ;
        // 编译图表
        var compiledGraph = stateGraph.compile();

        // 运行图表
        // `stream` 方法返回一个 AsyncGenerator。
        // 为简单起见，我们将收集结果。在实际应用中，您可能会在结果到达时对其进行处理。
        // 此处，执行后的最终状态是感兴趣的项。
        for (var item : compiledGraph.stream( Map.of( SimpleState.MESSAGES_KEY, "Let's, begin!" ) ) ) {

            System.out.println( item );
        }


    }
}
